package com.example.home.volunteer_service.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.home.R;
import com.example.home.volunteer_service.bean.ServiceNewsBean;

import java.util.List;

import static com.example.home.tool.StaticActi.ip;

public class ServiceNewsAdapter extends RecyclerView.Adapter<ServiceNewsAdapter.MyHoldy> {
    private Context context;
    private List<ServiceNewsBean.RowsBean> list;

    public ServiceNewsAdapter(Context context, List<ServiceNewsBean.RowsBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public ServiceNewsAdapter.MyHoldy onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_nes, null);
        return new ServiceNewsAdapter.MyHoldy(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ServiceNewsAdapter.MyHoldy holder, int position) {
        ServiceNewsBean.RowsBean rowsBean = list.get(position);
        Glide.with(context).load(ip+rowsBean.getImgUrl()).into(holder.imgUrl);
        holder.title.setText(rowsBean.getTitle());
        String summary = rowsBean.getSummary();
        if (summary.length() >= 25){

            holder.summary.setText(summary.substring(0,25)+"...");
        }else {
            holder.summary.setText(summary);
        }
        holder.createTime.setText(rowsBean.getCreateTime());
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class MyHoldy extends RecyclerView.ViewHolder {
        private ImageView imgUrl;
        private TextView title;
        private TextView summary;
        private TextView createTime;

        public MyHoldy(@NonNull View itemView) {
            super(itemView);

            imgUrl = itemView.findViewById(R.id.imgUrl);
            title = itemView.findViewById(R.id.title);
            summary = itemView.findViewById(R.id.summary);
            createTime = itemView.findViewById(R.id.createTime);
        }
    }
}
